package flinty.autocomplete.combobox.test;

import java.awt.EventQueue;
import java.util.LinkedList;
import java.util.List;

import javax.swing.JComboBox;
import javax.swing.ListCellRenderer;

import flinty.autocomplete.combobox.AsyncComboBoxModel;
import flinty.autocomplete.combobox.AutoCompleteComboBoxDecorator;
import flinty.autocomplete.combobox.AutoCompleteComboBoxDecorator.RendererFactory;
import flinty.autocomplete.combobox.DataProvider;
import flinty.autocomplete.combobox.ItemWrapper;
import flinty.autocomplete.combobox.StringConverter;

public class AsyncModelApplet extends BaseTestApplet {

	private static final long serialVersionUID = 6038797759813973904L;

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {

			@Override
			public void run() {
				AsyncModelApplet frame = new AsyncModelApplet();
				frame.setVisible(true);
			}
		});
	}

	@Override
	protected void decorateComboBox(JComboBox combo,
			final LinkedList<Integer> lst) {
		AutoCompleteComboBoxDecorator.decorate(
				combo,
				new AsyncComboBoxModel<Integer>(new DataProvider<Integer>() {

					@Override
					public List<Integer> getData(String pattern) {
						LinkedList<Integer> result = new LinkedList<Integer>();
						boolean empty = false;
						if (pattern == null || pattern.trim().length() == 0) {
							empty = true;
							result.add(null);
						}
						for (Integer item : lst) {
							if (empty || item.toString().contains(pattern)) {
								result.add(item);
							}
						}
						try {
							Thread.sleep(2000);
						} catch (InterruptedException e) {
							e.printStackTrace();
						}
						return result;
					}
				}, new StringConverter<Integer>() {

					@Override
					public String render(Integer object) {
						return object != null ? object.toString() : null;
					}
				}, "wait...", false),
				new RendererFactory() {

					@Override
					public ListCellRenderer createRenderer(
							ListCellRenderer originalRenderer) {
						return originalRenderer;
					}
				},
				new ItemWrapper<Integer>(Integer.valueOf(543221), Integer
						.toString(543221)));

	}

	@Override
	public String getAppletInfo() {
		return "simple list autocomplete demo";
	}
}
